package com.fortune.service.impl;


import com.fortune.common.paging.PagingData;
import com.fortune.dao.DUserMgtDao;
import com.fortune.dto.in.dUserMgt.DUserAddDto;
import com.fortune.dto.in.dUserMgt.DUserEditDto;
import com.fortune.dto.in.dUserMgt.DUserListDto;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.DUserMgtService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.dUserMgt.Positon;
import com.fortune.vo.dUserMgt.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class DUserMgtServiceImpl implements DUserMgtService {
	@Autowired
	private DUserMgtDao dUserMgtDao;


	@Override
	public PagingData<User> pagingData(String companyId, String dealerId, DUserListDto listDto) {
		List<User> list=dUserMgtDao.list(companyId,dealerId,listDto);
		int count=dUserMgtDao.count(companyId, dealerId, listDto);
		PagingData<User> pagingData=new PagingData<>(list,count);
		return  pagingData;
	}

	@Override
	public int editBan(String dealerId, String id) {
		return dUserMgtDao.editBan(dealerId, id);
	}

	@Override
	public int editAllow(String dealerId, String id) {
		return dUserMgtDao.editAllow(dealerId, id);
	}

	@Override
	public int editDel(String dealerId, String id) {
		return dUserMgtDao.editDel(dealerId, id);
	}

	@Override
	public int addEmployer(String dealerId, DUserAddDto dUserAddDto) {
		int checkPhone = dUserMgtDao.checkPhone(dealerId, dUserAddDto);
		if(checkPhone>0){
			return 2;
		}
		int checkNumber = dUserMgtDao.checkNumber(dealerId, dUserAddDto);
		if(checkNumber>0){
			return 3;
		}
		return dUserMgtDao.addEmployer(dealerId, dUserAddDto);
	}

	@Override
	@Transactional
	public int editEmployer(String dealerId,String id, DUserEditDto dUserEditDto) {
		Positon positon = dUserMgtDao.findPositionIsAdmin(dealerId,dUserEditDto.getRoleId());
		if(positon == null){
			return 4;
		}
		int checkPhone = dUserMgtDao.checkPhoneEdit(dealerId,id, dUserEditDto);
		if(checkPhone>0){
			return 2;
		}
		int checkNumber = dUserMgtDao.checkNumberEdit(dealerId,id, dUserEditDto);
		if(checkNumber>0){
			return 3;
		}
		if(positon.getId().equals("0000")){
			int oo = dUserMgtDao.editEmployers(dealerId,id,dUserEditDto.getName(),dUserEditDto.getPhone());
			if(oo > 0){
			  int oa =	dUserMgtDao.editAccount(dealerId,dUserEditDto);
			  if(oa > 0){
			  	return oo;
			  }else{
				  throw new UpdateInnerErrorException("编辑失败");
			  }
			}else{
				throw new UpdateInnerErrorException("编辑失败");
			}
		}
		return dUserMgtDao.editEmployer(dealerId,id,dUserEditDto);
	}

	@Override
	public int editPassword(String dealerId, String id, String oldPassword,String newPassword) {
		String findPassword=dUserMgtDao.findPassword(dealerId, id);
		if(!oldPassword.equals(findPassword)){
			int editPassword=-1;
			return editPassword;
		}
		int editPassword=dUserMgtDao.editPassword(dealerId, id, newPassword);
		return editPassword;
	}

	@Override
	public List<Positon> findListPosition(String did) {
		return dUserMgtDao.findListPosition(did);
	}

	@Override
	public int resetPassword(String did, String id) {
		int aa = dUserMgtDao.resetPassword(did,id);
		return aa;
	}
}
